;$(function(){
	
    var op = addUsers();
    op.createTable();
 
    
    
    function addUsers(){
        var rs = {};
     	function _render(msg){
     		$("table").empty()
        	var str1 ="<tr><td>编号</td><td>姓名</td><td>薪水</td><td>性别</td><td>生日</td></tr>"
        		$("table").append(str1);
              msg.forEach(function(item,index){
            	  var sex;
            	if(item.sex){
            		sex ="男";
            	}else{
            		sex = "女";
            	}
            	var date = new Date(item.birth);
            	
                var str2="<td>"
                +item.id+"</td><td>"+item.name+"</td><td>"+item.sal+"</td><td>"+sex+"</td><td>"+date+"</td>"
                
                var btn1 = $("<button class='btn btn-default'>删除</button>").on('click',function(){
                	rs.del(item.id);
                });
                var btn2 = $("<button type='button' class='btn btn-default' data-toggle='modal' data-target='#myModal'>更新</button>").on('click',function(){
                	rs.update(item);
                });
                var tr = $("<tr></tr>").append(str2);
                $("table").append(tr);
                tr.append(btn1);
                tr.append(btn2);
                });
     	}
     	
     	
     	rs.del=function(id){
     		j.ajax("DELETE", "deleteUser?id="+id, null,null, this,function(msg){
            	_render(msg);
            }); 
     		$.ajax({
                type:"DELETE",
                url:"http://localhost:8081/UserMVC/deleteUser?id="+id,
            })
                .done(function(msg){
                	_render(msg);
            }); 
     	}
     	
     	rs.update=function(item){
     		 $("#id").val(item.id);
     		$("#update").on("click",function(){
     			let user = $("#form2").serializeObject()
            	j.ajax("POST", "updateUser", user, {"Content-Type":"application/json"}, this,function(msg){
                	_render(msg);
                }); 
            });
     	}
     	rs.find=function(){
     		j.ajax("GET", "findAllUsers", null, null, this,function(msg){
                	_render(msg);
            }); 
     	}
        rs.createTable = function(){
        	rs.find();
            $("#add").on("click",function(){
            	var user = $("#form1").serializeObject();
            	j.ajax("POST", "addUser", user, {"Content-Type":"application/json"}, this,function(msg){
                	_render(msg);
                });     
            });
            
        }
        
        return rs;
    }
    
  
    
})
$.fn.serializeObject = function()
    	{
    	    var o = {};
    	    var a = this.serializeArray();
    	    $.each(a, function() {
    	        if (o[this.name] !== undefined) {
    	            if (!o[this.name].push) {
    	                o[this.name] = [o[this.name]];
    	            }
    	            o[this.name].push(this.value || '');
    	        } else {
    	            o[this.name] = this.value || '';
    	        }
    	    });
    	    return o;
    	};
    	(function(){
    		var obj={};
    		obj.ajax=function(type,url,data,headers,context,callback){
    			$.ajax({type:type,url:url,data:JSON.stringify(data),headers:headers,context:context})
    			.done(callback);
    		};
    		window.j=obj;
    	})();